package dao

import (
	"genealogy/models"
)

func GetAllPerson(Person map[string]interface{}) (error, []models.Person, int64) {
	var PersonData []models.Person
	page := Person["page"].(int)
	pageSize := Person["limit"].(int)
	searchName := Person["searchName"].(string)
	var total int64
	err := db.Table("person").Select("person_name, person_no, person_sex, person_area, birth_date, marry_date, die_date, update_time").Where("person_name like ? and is_del = false", searchName+"%").Order("person_no ASC").Count(&total).Offset((page - 1) * pageSize).Limit(pageSize).Find(&PersonData).Error
	return err, PersonData, total
}
func DelPerson(id string) error {
	err := db.Table("person").Where("person_no = ?", id).Update("is_del", true).Error
	return err
}

func UpdatePerson(Person models.Person) error {
	err := db.Table("person").Where("person_no = ?", Person.PersonNo).Updates(&Person).Error
	return err
}

func AddPerson(Person models.Person) error {
	err := db.Table("person").Select("person_name", "person_no", "person_sex", "person_area", "birth_date", "marry_date", "die_date", "update_time", "is_del").Create(&Person).Error
	return err
}
